package com.xiaoyu.lanling.feature.videomatch.data

import `in`.srain.cube.request.JsonData
import `in`.srain.cube.request.RequestDefaultHandler
import com.xiaoyu.base.event.NoReceiverJsonEvent
import com.xiaoyu.base.net.RequestCreator
import com.xiaoyu.lanling.common.config.HttpConfig
import com.xiaoyu.lanling.event.videomatch.*
import com.xiaoyu.lanling.feature.videomatch.datamodel.EvaluateEnum

class VideoMatchData {

    companion object {
        const val MATCH_STATUS_INIT = 0
        const val MATCH_STATUS_WAITING = 1
        const val MATCH_STATUS_MATCHED = 2
        const val MATCH_STATUS_EXIT = 3

        fun applyMatch(requestTag: Any) {
            RequestCreator.create<VideoMatchApplyMatchEvent>(requestTag, VideoMatchApplyMatchEvent::class.java)
                    .setPostEventWhenFail(true)
                    .setRequestUrl(HttpConfig.VIDEO_MATCH_APPLY_MATCH)
                    .enqueue()
        }

        fun exitMatch(requestTag: Any) {
            RequestCreator.create<VideoMatchExitMatchEvent>(requestTag, VideoMatchExitMatchEvent::class.java)
                    .setPostEventWhenFail(true)
                    .setRequestUrl(HttpConfig.VIDEO_MATCH_EXIT_MATCH)
                    .enqueue()
        }

        fun establishConnection(fromUid: String) {
            RequestCreator.create<NoReceiverJsonEvent>(Any(), NoReceiverJsonEvent::class.java)
                    .setRequestUrl(HttpConfig.VIDEO_MATCH_ESTABLISH_CONNECTION)
                    .addPostData("fromUid", fromUid)
                    .enqueue()
        }

        fun hangup(fuid: String?, type: String?) {
            RequestCreator.create<NoReceiverJsonEvent>(Any(), NoReceiverJsonEvent::class.java)
                    .setRequestUrl(HttpConfig.VIDEO_MATCH_HANG_UP)
                    .addPostData("anotherUid", fuid)
                    .addPostData("typeForLog", type)
                    .enqueue()
        }

        fun isInQueue(requestTag: Any) {
            RequestCreator.create<VideoMatchIsInQueueEvent>(requestTag, VideoMatchIsInQueueEvent::class.java)
                    .setRequestUrl(HttpConfig.VIDEO_MATCH_IN_QUEUE)
                    .enqueue()
        }

        fun checkEntryStatus(requestTag: Any, from: String) {
            RequestCreator.create<VideoMatchCheckEntryStatusEvent>(VideoMatchCheckEntryStatusEvent::class.java)
                    .setRequestUrl(HttpConfig.VIDEO_MATCH_CHECK_ENTRY_STATUS)
                    .setRequestHandler(object : RequestDefaultHandler<VideoMatchCheckEntryStatusEvent, JsonData>() {
                        override fun processOriginData(originData: JsonData): VideoMatchCheckEntryStatusEvent {
                            return VideoMatchCheckEntryStatusEvent(requestTag, originData.optJson("data"), from)
                        }

                        override fun onRequestFinish(event: VideoMatchCheckEntryStatusEvent?) {
                            event?.post()
                        }
                    })
                    .enqueue()
        }

        fun currentRank(requestTag: Any) {
            RequestCreator.create<VideoMatchCurrentRankEvent>(requestTag, VideoMatchCurrentRankEvent::class.java)
                    .setRequestUrl(HttpConfig.VIDEO_MATCH_CURRENT_RANK)
                    .enqueue()
        }

        fun matchEvaluate(requestTag: Any, uid: String, evaluate: EvaluateEnum) {
            RequestCreator.create<NoReceiverJsonEvent>(requestTag, NoReceiverJsonEvent::class.java)
                    .setRequestUrl(HttpConfig.VIDEO_MATCH_MATCH_EVALUATE)
                    .addPostData("uid", uid)
                    .addPostData("evaluate", evaluate.type)
                    .enqueue()
        }
    }
}